package yxy.game.pm2.command.stairway;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.StairwayMessage;
import yxy.game.pm2.module.stairway.StairwayHandle;

/** 天梯争雄 天梯选手信息 */
@MessageType(proto = Command.StairwayPlayerInfo, classtype = StairwayMessage.StairwayPlayerInfoRequest.class)
public class CMD_StairwayPlayerInfo extends Command<StairwayMessage.StairwayPlayerInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, StairwayMessage.StairwayPlayerInfoRequest request) {
		String playerCode = messageSource.getTag();
		int rank = request.getRank();

		StairwayMessage.StairwayPlayerInfoResponse.Builder response = StairwayMessage.StairwayPlayerInfoResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		boolean isOpen = StairwayHandle.isOpen(areaBean.getGroupId());
		if (!isOpen) {
			send(messageSource, response, Result.StairwayNotOpen);// 活动没有开始
			return;
		}



		StairwayHandle stairwayHandle = new StairwayHandle();
		Result result = stairwayHandle.playerInfo(areaBean.getGroupId(), rank, response);

		send(messageSource, response, result);
	}

	private void send(MessageSource messageSource, StairwayMessage.StairwayPlayerInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}